//################################################################################################################################//
//##                                                                                                                            ##//    
//## This control is implement for find the dentist                                                                             ##//
//##                                                                                                                            ##//
//## Author   : Prashant Tomar                                                                                                  ##//
//## Dated    : 06/08/2007                                                                                                      ##//
//##                                                                                                                            ##//    
//################################################################################################################################//

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using SiteManager;

public partial class includes_DntSearchControl : System.Web.UI.UserControl
{
    #region creating an instance of manager class and Local Variable
    Manager mgr = new Manager(true,"ConnectionString");
    string _fromPage = "";
    bool _isBlank = false;
    #endregion

    #region properties
    public string fromPage
    {
        set { _fromPage = value; }
        get { return _fromPage; }
    }
    public bool isBlank
    {
        get { return _isBlank; }
    }
    #endregion
        
    protected void Page_Load(object sender, EventArgs e)
    {
     //   Response.Write("-----" + this.fromPage);
        btnSubmit.Attributes.Add("onclick", "return " + this.ClientID + "chkError();");
        if (!Page.IsPostBack)
        {
            // fill drop downs
            mgr.BindList("select Id,Specialty from tbl_DentistSpecialty order by Specialty", "Specialty", "Id", ddlSpeciality, "select");
            mgr.BindList("select [countryname],countrycode from tbl_country where(countrycode='US')OR (countrycode='CA') order by [countryname]", "countryname", "countrycode", ddlCountry, "select");
           // mgr.BindList("select stateid,name from tbl_states order by name", "Name", "StateId", ddlState, "");
            ddlState.Items.Insert(0, new ListItem("State/Province", "0"));

         // If condition edited as to remove HOSPITAL dropdown from patient_rate page
         //   if (this.fromPage != null && this.fromPage != "" && this.fromPage.ToString() == "RateDoctors")
            if (this.fromPage != null && this.fromPage != "" && this.fromPage.ToString() == "RateDoctors")
            {
                tr_Hospital.Visible = true;
                tr_Insurance.Visible = true;
                mgr.BindList("select id,name from tbl_InsuranceCompanies order by name", "name", "Id",
                    ddlInsurance, "");
                ddlInsurance.Items.Insert(0, new ListItem("--Any--", ""));
                mgr.BindList("select Id,HName from tbl_hospitals where isdelete=0 and HName<>'' order by hname",
                    "HName", "Id", ddlhospital, "");
                ddlhospital.Items.Insert(0, new ListItem("--Any--", ""));
            }
            //else
            //{
            //    tr_Insurance.Visible = true;
            //    mgr.BindList("select id,name from tbl_InsuranceCompanies order by name", "name", "Id",
            //        ddlInsurance, "");
            //    ddlInsurance.Items.Insert(0, new ListItem("--Any--", ""));
            //    mgr.BindList("select Id,HName from tbl_hospitals where isdelete=0 and HName<>'' order by hname",
            //        "HName", "Id", ddlhospital, "");
            //    ddlhospital.Items.Insert(0, new ListItem("--Any--", ""));
            //}
            
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Session["pageFromOrAction"] = this.fromPage;
        Session["sDoctorsId"] = "";
        Session["FeaDoctorsId"] = "";
        Session["Top3"] = "";

        Session["sDentistsId"] = "";
        Session["PreDoctorsId"] = "";
        Session["EnFeaDoctorsId"] = "";
        Session["StdFeaDoctorsId"] = "";
        Session["Travel_Dis"] = "";
        Session["Top3"] = "";
        Session["ZipCode"] = "";
        Session["City"] = "";
        Session["State"] = "";
        Session["FteaturedDentistsId"] = "";
        Session["Speciality"] = "";
        //if (ddlSpeciality.Items.Count > 1)
        //{
        //    if (ddlSpeciality.SelectedIndex > 0)
        //        Session["Speciality"] = ddlSpeciality.SelectedItem.Text;
        //}

        string qstring = "";

        if (ddlSpeciality.Items.Count > 1)
        {
            if (ddlSpeciality.SelectedIndex > 0)
            {
                Session["Speciality"] = ddlSpeciality.SelectedItem.Text.Trim().Replace(" ", "_");
                //qstring = "?specialty=" + Session["Speciality"];
                qstring = Session["Speciality"].ToString();
            }
        }

        

        if (ddlState.Items.Count > 1)
        {
            if (ddlState.SelectedIndex > 0)
            {
                if (qstring == "")
                {
                    //qstring = "?state=" + ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                    qstring = ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                }
                else
                {
                    //qstring = qstring + "&state=" + ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                    qstring = qstring + "/" + ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                }
            }
        }

        if (txtCity.Text.Trim() != "")
        {
            if (qstring == "")
            {
                //qstring = "?city=" + txtCity.Text.Trim().Replace(" ", "_");
                qstring = txtCity.Text.Trim().Replace(" ", "_");
            }
            else
            {
               // qstring = qstring + "&city=" + txtCity.Text.Trim().Replace(" ", "_");
                qstring = qstring + "/" + txtCity.Text.Trim().Replace(" ", "_");
            }
        }

        if (txtZipCode.Text.Trim() != "")
        {
            if (qstring == "")
            {
                //qstring = "?zip=" + txtZipCode.Text.Trim().Replace(" ", "_");
                qstring = txtZipCode.Text.Trim().Replace(" ", "_");
            }
            else
            {
                //qstring = qstring + "&zip=" + txtZipCode.Text.Trim().Replace(" ", "_");
                qstring = qstring + "/" + txtZipCode.Text.Trim().Replace(" ", "_");
            }
        }

        if (txtLName.Text.Trim().Equals("Last Name:"))
            txtLName.Text = "";

        

        #region calling stored procedure
        // get the results of the search
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[9];
            sqlParam[8] = new SqlParameter("@CountryId", SqlDbType.VarChar, 2);
            sqlParam[0] = new SqlParameter("@state", SqlDbType.VarChar, 2);
            sqlParam[1] = new SqlParameter("@city", SqlDbType.VarChar, 50);
            sqlParam[2] = new SqlParameter("@zip", SqlDbType.VarChar, 10);
            sqlParam[3] = new SqlParameter("@LName", SqlDbType.VarChar, 50);
            sqlParam[4] = new SqlParameter("@FName", SqlDbType.VarChar, 50);
            sqlParam[5] = new SqlParameter("@spec_id", SqlDbType.Int);
            sqlParam[6] = new SqlParameter("@hospital", SqlDbType.VarChar, 200);
            sqlParam[7] = new SqlParameter("@insurance", SqlDbType.VarChar, 200);

            //sqlParam[4] = new SqlParameter("@rating", SqlDbType.Int);
            //sqlParam[7] = new SqlParameter("@subspec_id", SqlDbType.Int);
            sqlParam[8].Value = ddlCountry.SelectedValue;
            sqlParam[0].Value = ddlState.SelectedItem.Value.Equals("0") ? "" : ddlState.SelectedItem.Value;
            sqlParam[1].Value = txtCity.Text.Trim();
            sqlParam[2].Value = txtZipCode.Text.Trim();
            sqlParam[3].Value = txtLName.Text.Trim();
            sqlParam[4].Value = txtFName.Text.Trim();
            sqlParam[5].Value = ddlSpeciality.SelectedItem.Value;

            if (ddlhospital.Visible == true)
                sqlParam[6].Value = ddlhospital.SelectedItem.Value;

            if (ddlInsurance.Visible)
                sqlParam[7].Value = ddlInsurance.SelectedItem.Value;
            DataSet ds_result = mgr.ExecuteQuery("[HomepageDentAdvanceSearch]", sqlParam, "tbl_srch");
        #endregion

            //string DoctorsNonFeaIds = GetDentistsId(ds_result);
            //string DoctorsFeaturedIds = GetDentistsIdFeat(ds_result, 1);
            //// get top 3
            //string Top3DrFeaturedIds = GetDentistsIdFeat(ds_result, 2);
            string DoctorsNonFeaIds = GetDentistId(ds_result);
            string PremiumDoctorsFeaturedIds = GetPremiumDentistsIdFeat(ds_result, 1);
            string EnhancedDoctorsFeaturedIds = GetEnhancedDentistsIdFeat(ds_result, 1);
            string StandardDoctorsFeaturedIds = GetStandardDentistsIdFeat(ds_result, 1);
            string FeaturedDentistIds = PremiumDoctorsFeaturedIds + EnhancedDoctorsFeaturedIds + StandardDoctorsFeaturedIds;
            string Top3DrFeaturedIds = GetPremiumDentistsIdFeat(ds_result, 1);
            //if (this._fromPage != "CommunityPage")
            //{
                if ((DoctorsNonFeaIds.Trim().Length > 0) || (PremiumDoctorsFeaturedIds.Trim().Length > 0) || (Top3DrFeaturedIds.Trim().Length > 0) || (EnhancedDoctorsFeaturedIds.Trim().Length > 0) || (StandardDoctorsFeaturedIds.Trim().Length > 0))
                {
                    //Session["sDoctorsId"] = DoctorsNonFeaIds;
                    //Session["PreDoctorsId"] = PremiumDoctorsFeaturedIds;
                    //Session["EnFeaDoctorsId"] = EnhancedDoctorsFeaturedIds;
                    //Session["StdFeaDoctorsId"] = StandardDoctorsFeaturedIds;
                    Session["Top3"] = Top3DrFeaturedIds;
                    // Session["pageFromOrAction"] = "home";

                    // 8th March
                    Session["PreviousSpeciality"] = "";
                    Session["PreviousSubSpeciality"] = "";

                    if (ddlSpeciality.SelectedIndex > 0)
                        Session["PreviousSpeciality"] = ddlSpeciality.SelectedItem.Text;
                    //


                    // 11th Dec

                    Session["sDentistId"] = DoctorsNonFeaIds; // DoctorsNonFeaIds;
                    Session["PreDentistId"] = PremiumDoctorsFeaturedIds; // PremiumDoctorsFeaturedIds;
                    Session["EnDentistId"] = EnhancedDoctorsFeaturedIds; // EnhancedDoctorsFeaturedIds;
                    Session["StdDentistId"] = StandardDoctorsFeaturedIds; // StandardDoctorsFeaturedIds;

                    Session["Top3"] = Top3DrFeaturedIds;
                    //Response.Redirect(Session["strHttp"] + "SearchResults_dentist.aspx", true);

                    qstring = qstring.ToString().Trim().Replace("(", "_");
                    qstring = qstring.ToString().Trim().Replace(")", "_");
                    qstring = qstring.ToString().Trim().Replace("_&_", "_");
                    qstring = qstring.ToString().Trim().Replace("[", "_");
                    qstring = qstring.ToString().Trim().Replace("]", "_");
                    qstring = qstring.ToString().Trim().Replace("\\", "_");
                    qstring = qstring.ToString().Trim().Replace("//", "_");
                    qstring = qstring.ToString().Trim().Replace("-", "_");
                    qstring = qstring.ToString().Trim().Replace("___", "_");
                    qstring = qstring.ToString().Trim().Replace("__", "_");

                    //Response.Redirect(Session["strHttp"] + "SearchResultsDent.aspx" + qstring, true);

                    if (qstring != "")
                    {
                        if (this._fromPage == "CommunityPage")
                        {
                            Response.Redirect(Session["strHttp"] + "SearchResultDenCom.aspx", false);
                        }
                        else
                        {
                            Response.Redirect(Session["strHttp"] + qstring.ToString().Trim() + "/dentist.aspx", false);
                        }
                    }
                    else
                    {
                        Response.Redirect(Session["strHttp"] + "search" + "/dentist.aspx", false);
                    }
                }
                else
                {
                    _isBlank = true;
                    hfIsToShowMessage.Value = "1";
                }
           // }
            //else
            //{
            //    if (FeaturedDentistIds.Trim().Length > 0)
            //    {
            //        Response.Redirect(Session["strHttp"] + "SearchResultDenCom.aspx", false);
            //    }
            //    else
            //    {
            //        hfIsToShowMessage.Value = "1";
            //    }


            //}
        }
            
        catch (Exception ex)
        {
            Response.Write(ex.ToString());

        }
    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        string countryID = ddlCountry.SelectedValue;
        // mgr.BindList("select countrycode,countryname from tbl_country", "countryname", "countrycode", ddlCountry, "select");
        mgr.BindList("select stateid,name from tbl_states where CountryID='" + countryID + "'  order by name", "Name", "StateId", ddlState, "--Any--");
        if (ddlCountry.SelectedValue == "US")
        {
            ddlState.Items.Insert(0, new ListItem("Select a State", "0"));
        }
        else
        {
            ddlState.Items.Insert(0, new ListItem("Select a Province", "0"));
        }
    }
    #region general functions

    /// <summary>
    /// get the doctor id for the doctors in the result
    /// </summary>
    /// <param name="ds"></param>
    /// <returns></returns>
    //private string GetDentistsId(DataSet ds)
    //{
    //    string DoctorsIds = "";

    //    try
    //    {
    //        if (ds.Tables[0].Rows.Count > 0)
    //        {
    //            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    //            {
    //                if (i == 0)
    //                {
    //                    DoctorsIds = ds.Tables[0].Rows[i][0].ToString();
    //                }
    //                else
    //                {
    //                    DoctorsIds += "," + ds.Tables[0].Rows[i][0].ToString();
    //                }
    //            }
    //        }
    //    }
    //    catch
    //    {
    //        DoctorsIds = "";
    //    }

    //    return DoctorsIds;


    //}


    //#region get featured dr id s j=2 means get top 3
    //private string GetDentistsIdFeat(DataSet ds, int j)
    //{
    //    string DoctorsIds = "";
    //    int count = 0;
    //    int k = 0;
    //    try
    //    {
    //        if (ds.Tables[1].Rows.Count > 0)
    //        {
    //            if (j == 2)
    //            {
    //                if (ds.Tables[1].Rows.Count > 3)
    //                {
    //                    count = 3;
    //                }
    //                else
    //                {
    //                    count = ds.Tables[1].Rows.Count;
    //                }

    //                k = 0;
    //            }
    //            else
    //            {
    //                count = ds.Tables[1].Rows.Count;
    //                k = 3;
    //            }
    //            //ds.Tables[1].Rows.Count
    //            for (int i = k; i < count; i++)
    //            {
    //                if ((i == 0) || (i == 3))
    //                {
    //                    DoctorsIds = ds.Tables[1].Rows[i][0].ToString();
    //                }
    //                else
    //                {
    //                    DoctorsIds += "," + ds.Tables[1].Rows[i][0].ToString();
    //                }
    //            }
    //        }
    //    }
    //    catch
    //    {
    //        DoctorsIds = "";
    //    }
    //    return DoctorsIds;
    //}

        private string GetDentistId(DataSet ds)
    {
        string DentistIds = "";
        try
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (i == 0)
                    { DentistIds = ds.Tables[0].Rows[i][0].ToString(); }
                    else
                    { DentistIds += "," + ds.Tables[0].Rows[i][0].ToString(); }
                }
            }
        }
        catch
        {
            DentistIds = "";
        }
        return DentistIds;
    }


    /// <summary>
    /// get featured dr id s j=2 means get top 3
    /// </summary>
    /// <param name="ds"></param>
    /// <param name="j"></param>
    /// <returns></returns>
    private string GetPremiumDentistsIdFeat(DataSet ds, int j)
    {
        string DentistsIds = "";
        int count = 0;
        int k = 0;
        try
        {
            if (ds.Tables[1].Rows.Count > 0)
            {
                if (j == 2)
                {
                    if (ds.Tables[1].Rows.Count > 3)
                    {
                        count = 3;
                    }
                    else
                    {
                        count = ds.Tables[1].Rows.Count;
                    }

                    k = 0;
                }
                else
                {
                    count = ds.Tables[1].Rows.Count;
                    //    k = 3;
                }
                //ds.Tables[1].Rows.Count
                for (int i = k; i < count; i++)
                {
                    if ((i == 0) || (i == 3))
                    {
                        DentistsIds = ds.Tables[1].Rows[i][0].ToString();
                    }
                    else
                    {
                        DentistsIds += "," + ds.Tables[1].Rows[i][0].ToString();
                    }
                }
            }
        }
        catch
        {
            DentistsIds = "";
        }

        return DentistsIds;


    }
    private string GetEnhancedDentistsIdFeat(DataSet ds, int j)
    {
        string DoctorsIds = "";

        try
        {

            // DataSet ds = mgr.ExecuteQuery(sql, "tblResults");
            if (ds.Tables[2].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[2].Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        DoctorsIds = ds.Tables[2].Rows[i][0].ToString();
                    }
                    else
                    {
                        DoctorsIds += "," + ds.Tables[2].Rows[i][0].ToString();
                    }
                }
            }
        }


        catch
        {
            DoctorsIds = "";
        }

        return DoctorsIds;


    }
    private string GetStandardDentistsIdFeat(DataSet ds, int j)
    {
        string DoctorsIds = "";

        try
        {

            // DataSet ds = mgr.ExecuteQuery(sql, "tblResults");
            if (ds.Tables[3].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[3].Rows.Count; i++)
                {
                    if (i == 0)
                    {
                        DoctorsIds = ds.Tables[3].Rows[i][0].ToString();
                    }
                    else
                    {
                        DoctorsIds += "," + ds.Tables[3].Rows[i][0].ToString();
                    }
                }
            }
        }


        catch
        {
            DoctorsIds = "";
        }

        return DoctorsIds;


    }
    #endregion


    //#endregion
}
